﻿using EquipSecs300;
using EquipSecs300.Parameter;
using log4net.Util;
using SecsEquip;
using SecsEquip300;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace EquipSecs300
{
    public class GEM_LoadPortSub : SecsEquipEx
    {
        int m_nPortNo = 0;
        public PortTransferState_KE loadPortTransferState_KE;
        public CarrierIDStatus_KE carrierIDStatus_KE;
        public CarrierSlotMapStatus_KE carrierSlotMapStatus_KE;

        internal GEM_LoadPort LoadPortBase { get; set; }
        public bool SamplesConfirm { get; set; }
        public string CarrierID { get; set; }
        public List<ushort> MapSlotResult { get; set; }

        public GEM_LoadPortSub(GEM_LoadPort baseloadPort, SecsEquipHelper secsEquipHelper)
        {
            m_pName = "Carrier";
            this.LoadPortBase = baseloadPort;
            this.SecsEquipHelper = secsEquipHelper;
        }

        // 设置端口号
        public void SetPortNo(int nPortNo)
        {
            m_nPortNo = nPortNo;
        }

        public int GetPortNo()
        {
            return m_nPortNo;
        }

        public override void UpdateData()
        {

        }

        /// <summary>
        /// 响应Load Port状态改变事件TransferStateChange
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="nPort"></param>
        /// <param name="nState"></param>
        public void OnCMSTransferStateChangeProc(object sender, int nPort, LoadPortTransferState nState)
        {
            loadPortTransferState_KE = (PortTransferState_KE)nState;

        }

        /// <summary>
        /// 响应状态改变CarrierIDStatus
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="nPort"></param>
        /// <param name="pCarrierID"></param>
        /// <param name="nState"></param>
        public void OnCmsCarrierIDStatus(object sender, int nPort, string pCarrierID, CarrierIDStatus nState)
        {
            carrierIDStatus_KE = (CarrierIDStatus_KE)nState;
            if (carrierIDStatus_KE == CarrierIDStatus_KE.VERIFIED_OK)
            {
                if (nPort == 1)
                {
                    EquipSecs300.GlobalVar.AcceptCassette["Port_1"] = true;
                }
                else if (nPort == 2)
                {
                    EquipSecs300.GlobalVar.AcceptCassette["Port_2"] = true;
                }
            }
            else if (carrierIDStatus_KE == CarrierIDStatus_KE.VERIFY_FAILED)
            {
                if (nPort == 1)
                {
                    EquipSecs300.GlobalVar.RejectCassette["Port_1"] = true;
                }
                else if (nPort == 2)
                {
                    EquipSecs300.GlobalVar.RejectCassette["Port_2"] = true;
                }
            }
        }

        /// <summary>
        /// 响应状态改变SlotMapStatus
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="nPort"></param>
        /// <param name="pCarrier"></param>
        public void OnCmsCarrierSlotMapStatus(object sender, int nPort, Carrier pCarrier)
        {
            if (CarrierSlotMapStatus.VERIFIED_OK == pCarrier.nSlotMapStatus
                && pCarrier.pCarrierID.Equals(CarrierID))
            {
                carrierSlotMapStatus_KE = (CarrierSlotMapStatus_KE)CarrierSlotMapStatus.VERIFIED_OK;
            }
            else
            {
                carrierSlotMapStatus_KE = (CarrierSlotMapStatus_KE)pCarrier.nSlotMapStatus;
            }

            if (carrierSlotMapStatus_KE == CarrierSlotMapStatus_KE.VERIFIED_OK)
            {
                if (nPort == 1)
                {
                    EquipSecs300.GlobalVar.AcceptCassette["Port_1"] = true;
                }
                else if (nPort == 2)
                {
                    EquipSecs300.GlobalVar.AcceptCassette["Port_2"] = true;
                }
            }
            else if (carrierSlotMapStatus_KE == CarrierSlotMapStatus_KE.VERIFY_FAILED)
            {
                if (nPort == 1)
                {
                    EquipSecs300.GlobalVar.RejectCassette["Port_1"] = true;
                }
                else if (nPort == 2)
                {
                    EquipSecs300.GlobalVar.RejectCassette["Port_2"] = true;
                }
            }
        }

        public bool GetLotIDBysLotNO(int sLot, out string strLotID)
        {
            strLotID = "";
            if (LoadPortBase.PortInfo[m_nPortNo].CarrierInfo == null)
            {
                return false;
            }
            for (int i = 0; i < LoadPortBase.PortInfo[m_nPortNo].CarrierInfo.pSlotData.Length; i++)
            {
                if (LoadPortBase.PortInfo[m_nPortNo].CarrierInfo.pSlotData[i].nSlot == sLot)
                {
                    strLotID = LoadPortBase.PortInfo[m_nPortNo].CarrierInfo.pSlotData[i].pLotID;
                }
            }
            return true;
        }
        public bool GetLotNOBySLotNO(List<bool> listBool, out string strLotID)
        {
            List<int> intList = UtilitiesConvert.ConvertBoolListToIntList(listBool);
            if (intList == null)
            {
                strLotID = "";
                return false;
            }
            bool b = GetLotIDBysLotNO(intList[0], out strLotID);
            if (b)
            {
                return true;
            }
            else { return false; }
        }

        public string GetWaferIDBySLotNO(int sLot)
        {
            string WaferID = "";
            for (int i = 0; i < LoadPortBase.PortInfo[m_nPortNo].CarrierInfo.pSlotData.Length; i++)
            {
                if (LoadPortBase.PortInfo[m_nPortNo].CarrierInfo.pSlotData[i].nSlot == sLot)
                {
                    WaferID = LoadPortBase.PortInfo[m_nPortNo].CarrierInfo.pSlotData[i].pSubstrateID;
                }
            }
            return WaferID;
        }

        /// <summary>
        /// 删除Local的Carrier信息
        /// </summary>
        private void DeleteCarrierData()
        {
            //LoadPortBase.CarrierInfo.Remove(m_nPortNo);
            LoadPortBase.PortInfo[m_nPortNo] = new PortInfo();
            LoadPortBase.Save();
        }


        /// <summary>
        /// 本地Mapping结果与EAP校验
        /// </summary>
        /// <param name="mappingRes"></param>
        /// <returns></returns>
        public string VerifyCarrierMapRes(List<ushort> mappingRes)
        {
            if (mappingRes.Count == 0)
            {
                return "2";
            }
            if (LoadPortBase.PortInfo[m_nPortNo].CarrierInfo == null)
            {
                return "2";
            }
            var carrierSlotDataOrder = LoadPortBase.PortInfo[m_nPortNo].CarrierInfo.pSlotData.OrderBy(n => n.nSlot).ToArray();
            for (int i = 0; i < mappingRes.Count; i++)
            {
                if (mappingRes[i] == 0)
                {
                    if (carrierSlotDataOrder[i].nSlotMapEnum != SlotMap.EMPTY)
                    {
                        return "2";
                    }
                }
                else if (mappingRes[i] == 1)
                {
                    if (carrierSlotDataOrder[i].nSlotMapEnum != SlotMap.CORRECTLY_OCCUPIED)
                    {
                        return "2";
                    }
                }

            }
            return "1";
        }


        //其他内容

        private void SetFoupValue(PortNO_KE portNO_KE, PortFoupStatus_KE portFoupStatus_KE, string portFoupID)
        {
            SecsEquipHelper.Variables.SetValue(10199, ((int)portNO_KE).ToString());
            SecsEquipHelper.Variables.SetValue(10200, ((int)portFoupStatus_KE).ToString());
            SecsEquipHelper.Variables.SetValue(10201, portFoupID);
        }

        #region 事件列表

        private bool LoadPortLoading_B;
        public void Ev_LoadPortLoading_R()
        {
            LoadPortLoading_B = false;
        }
        /// <summary>
        /// 上料过程
        /// </summary>
        /// <param name="m_nPortNo"></param>
        public void Ev_LoadPortLoading()
        {
            if (LoadPortLoading_B) { return; }
            m_pSecs.CmsLoadPortLoading(m_nPortNo);
            LogEvenMsg("Ev_LoadPortLoading");
            LoadPortLoading_B = true;
        }

        private bool LoadPortUnLoading_B;
        public void Ev_LoadPortUnLoading_R()
        {
            LoadPortUnLoading_B = false;
        }
        /// <summary>
        /// 下料过程
        /// </summary>
        /// <param name="m_nPortNo"></param>
        public void Ev_LoadPortUnLoading()
        {
            if (LoadPortUnLoading_B) { return; }
            m_pSecs.CmsLoadPortUnloading(m_nPortNo);
            LogEvenMsg("Ev_LoadPortUnLoading");
            LoadPortUnLoading_B = true;
        }


        private bool BoxCarrierDocked_B;
        public void Ev_BoxCarrierDocked_R()
        {
            BoxCarrierDocked_B = false;
        }
        /// <summary>
        /// 料盒Docked
        /// </summary>
        /// <param name="m_nPortNo"></param>
        public void Ev_BoxCarrierDocked()
        {
            if (BoxCarrierDocked_B) { return; }
            m_pSecs.CmsLoadPortDocked(m_nPortNo);
            LogEvenMsg("Ev_BoxCarrierDocked");
            BoxCarrierDocked_B = true;
        }
        private bool BoxCarrierUnDocked_B;
        public void Ev_BoxCarrierUnDocked_R()
        {
            BoxCarrierUnDocked_B = false;
        }
        /// <summary>
        /// 料盒UnDocked
        /// </summary>
        /// <param name="m_nPortNo"></param>
        public void Ev_BoxCarrierUnDocked()
        {
            if (BoxCarrierUnDocked_B) { return; }
            m_pSecs.CmsLoadPortUndocked(m_nPortNo);
            LogEvenMsg("Ev_BoxCarrierUnDocked");
            BoxCarrierUnDocked_B = true;
        }


        private bool loadPortLoadReady_B;
        public void Ev_LoadPortLoadReady_R()
        {
            loadPortLoadReady_B = false;
        }

        /// <summary>
        /// 准备上料
        /// </summary>
        /// <param name="portNO_KE"></param>
        /// <param name="portWaferStatus_KE"></param>
        /// <param name="portFoupID"></param>
        public void Ev_LoadPortLoadReady(PortFoupStatus_KE portFoupStatus_KE, string portFoupID)
        {
            if (loadPortLoadReady_B) { return; }
            DeleteCarrierData();
            SetFoupValue((PortNO_KE)m_nPortNo, portFoupStatus_KE, portFoupID);
            if (m_nPortNo == 1)
            {
                SecsEquipHelper.Variables.SetValue(10190, ((int)PortStatus_KE.ReadyToLoad).ToString());
            }
            else if (m_nPortNo == 2)
            {
                SecsEquipHelper.Variables.SetValue(10191, ((int)PortStatus_KE.ReadyToLoad).ToString());
            }
            if (SecsDataHelper.Function.Gem300Enable)
            {
                m_pSecs.CmsLoadPortLoadReady(m_nPortNo);
                LogEvenMsg("1888_Ev_LoadPortLoadReady");
            }
            else
            {
                SecsEquipHelper.Even.PosEvent(30079);
            }

            loadPortLoadReady_B = true;
        }

        private bool LoadPortUnloadReady_B;
        public void Ev_LoadPortUnloadReady_R()
        {
            LoadPortUnloadReady_B = false;
        }
        /// <summary>
        /// 准备下料
        /// </summary>
        /// <param name="portNO_KE"></param>
        /// <param name="portWaferStatus_KE"></param>
        /// <param name="portFoupID"></param>
        public void Ev_LoadPortUnloadReady(PortFoupStatus_KE portFoupStatus_KE, string portFoupID)
        {
            if (LoadPortUnloadReady_B) { return; }
            SetFoupValue((PortNO_KE)m_nPortNo, portFoupStatus_KE, portFoupID);
            if (m_nPortNo == 1)
            {
                SecsEquipHelper.Variables.SetValue(10190, ((int)PortStatus_KE.ReadyToUnload).ToString());
            }
            else if (m_nPortNo == 2)
            {
                SecsEquipHelper.Variables.SetValue(10191, ((int)PortStatus_KE.ReadyToUnload).ToString());
            }
            if (SecsDataHelper.Function.Gem300Enable)
            {
                m_pSecs.CmsLoadPortUnloadReady(m_nPortNo);
                LogEvenMsg("1889_Ev_LoadPortUnloadReady");
            }
            else
            {
                SecsEquipHelper.Even.PosEvent(30080);
            }

            LoadPortUnloadReady_B = true;
        }


        private bool LoadPortLoaded_B;
        public void Ev_LoadPortLoaded_R()
        {
            LoadPortLoaded_B = false;
        }
        /// <summary>
        /// 上料完成
        /// </summary>
        /// <param name="portNO_KE"></param>
        /// <param name="portWaferStatus_KE"></param>
        /// <param name="portFoupID"></param>
        public void Ev_LoadPortLoaded(PortFoupStatus_KE loadPortFoupStatus_KE, string portFoupID)
        {
            if (LoadPortLoaded_B) { return; }
            SetFoupValue((PortNO_KE)m_nPortNo, loadPortFoupStatus_KE, portFoupID);
            if (m_nPortNo == 1)
            {
                SecsEquipHelper.Variables.SetValue(10190, ((int)PortStatus_KE.TransferBlocked).ToString());
            }
            else if (m_nPortNo == 2)
            {
                SecsEquipHelper.Variables.SetValue(10191, ((int)PortStatus_KE.TransferBlocked).ToString());
            }
            if (SecsDataHelper.Function.Gem300Enable)
            {
                m_pSecs.CmsLoadPortLoaded(m_nPortNo);
                LogEvenMsg("1835_Ev_LoadPortLoaded");
            }
            else
            {
                SecsEquipHelper.Even.PosEvent(30071);
            }

            LoadPortLoaded_B = true;
        }

        private bool BoxCarrierClamped_B;
        public void Ev_BoxCarrierClamped_R()
        {
            BoxCarrierClamped_B = false;
        }
        /// <summary>
        /// Load Port锁紧料盒
        /// </summary>
        /// <param name="portNO_KE"></param>
        /// <param name="portWaferStatus_KE"></param>
        /// <param name="portFoupID"></param>
        public void Ev_BoxCarrierClamped(PortFoupStatus_KE portFoupStatus_KE, string portFoupID)
        {
            if (BoxCarrierClamped_B) { return; }
            SetFoupValue((PortNO_KE)m_nPortNo, portFoupStatus_KE, portFoupID);
            if (SecsDataHelper.Function.Gem300Enable)
            {
                m_pSecs.CmsLoadPortClamped(m_nPortNo);
                LogEvenMsg("1800_Ev_BoxCarrierClamped");
            }
            else
            {
                SecsEquipHelper.Even.PosEvent(30072);
            }

            BoxCarrierClamped_B = true;
        }

        private bool ReadCarrierSucc_B;
        public void Ev_ReadCarrierSucc_R()
        {
            ReadCarrierSucc_B = false;
        }
        /// <summary>
        /// 读取料盒ID成功
        /// </summary>
        /// <param name="portNO_KE"></param>
        /// <param name="portWaferStatus_KE"></param>
        /// <param name="portFoupID"></param>
        public void Ev_ReadCarrierSucc(PortFoupStatus_KE portFoupStatus_KE, string portFoupID)
        {
            if (ReadCarrierSucc_B) { return; }
            if (portFoupID == null || portFoupID.Length == 0)
            {
                LogEvenMsg("Error: Ev_ReadCarrier -> Carrier ID为空");
                MessageBox.Show("Error: Ev_ReadCarrier -> Carrier ID为空");
                return;
            }
            CarrierID = portFoupID;

            #region 保存Port信息
            LoadPortBase.PortInfo[m_nPortNo].CarrierID = CarrierID;
            LoadPortBase.Save();
            #endregion

            SetFoupValue((PortNO_KE)m_nPortNo, portFoupStatus_KE, portFoupID);
            if (m_nPortNo == 1)
            {
                SecsEquipHelper.Variables.SetValue(10354, portFoupID);
            }
            else if (m_nPortNo == 2)
            {
                SecsEquipHelper.Variables.SetValue(10355, portFoupID);
            }
            if (SecsDataHelper.Function.Gem300Enable)
            {
                RcResult rc = m_pSecs.CmsLoadPortReadIDSucc(m_nPortNo, portFoupID);//CEID = 1825
                LogEvenMsg("1825_Ev_ReadCarrierSucc");
                if (rc != 0)
                {
                    LogEvenMsg($"Error: 1825_Ev_ReadCarrier  {rc.rc.ToString()} : {rc.result.ToString()}");
                    Task.Run(() => { MessageBox.Show($"Error: 1825_Ev_ReadCarrier  {rc.rc.ToString()} : {rc.result.ToString()}"); });
                }
            }
            else
            {
                SecsEquipHelper.Even.PosEvent(30073);
            }
            ReadCarrierSucc_B = true;
        }

        private bool ReadCarrierFail_B;
        public void Ev_ReadCarrierFail_R()
        {
            ReadCarrierFail_B = false;
        }
        /// <summary>
        /// 读取料盒ID失败
        /// </summary>
        /// <param name="portNO_KE"></param>
        /// <param name="portWaferStatus_KE"></param>
        /// <param name="portFoupID"></param>
        public void Ev_ReadCarrierFail(PortFoupStatus_KE portFoupStatus_KE, string portFoupID)
        {
            if (ReadCarrierFail_B) { return; }
            SetFoupValue((PortNO_KE)m_nPortNo, portFoupStatus_KE, portFoupID);
            if (SecsDataHelper.Function.Gem300Enable)
            {
                m_pSecs.CmsLoadPortReadIDFail(m_nPortNo);
                LogEvenMsg("1905_Ev_ReadCarrierFail");
            }
            else
            {
                SecsEquipHelper.Even.PosEvent(30078);
            }

            ReadCarrierFail_B = true;
        }

        private bool BoxCarrierOpened_B;
        public void Ev_BoxCarrierOpened_R()
        {
            BoxCarrierOpened_B = false;
        }
        /// <summary>
        /// 料盒门打开
        /// </summary>
        /// <param name="portNO_KE"></param>
        /// <param name="portWaferStatus_KE"></param>
        /// <param name="portFoupID"></param>
        public void Ev_BoxCarrierOpened(PortFoupStatus_KE portFoupStatus_KE, string portFoupID)
        {
            if (BoxCarrierOpened_B) { return; }
            SetFoupValue((PortNO_KE)m_nPortNo, portFoupStatus_KE, portFoupID);
            if (SecsDataHelper.Function.Gem300Enable)
            {
                m_pSecs.CmsLoadPortOpened(m_nPortNo);
                LogEvenMsg("1802_Ev_BoxCarrierOpened");
            }
            else
            {
                SecsEquipHelper.Even.PosEvent(30074);
            }

            BoxCarrierOpened_B = true;
        }

        private bool BoxCarrierClosed_B;
        public void Ev_BoxCarrierClosed_R()
        {
            BoxCarrierClosed_B = false;
        }
        /// <summary>
        /// 料盒门关闭
        /// </summary>
        /// <param name="portNO_KE"></param>
        /// <param name="portWaferStatus_KE"></param>
        /// <param name="portFoupID"></param>
        public void Ev_BoxCarrierClosed(PortFoupStatus_KE portFoupStatus_KE, string portFoupID)
        {
            if (BoxCarrierClosed_B) { return; }
            SetFoupValue((PortNO_KE)m_nPortNo, portFoupStatus_KE, portFoupID);
            if (SecsDataHelper.Function.Gem300Enable)
            {
                m_pSecs.CmsLoadPortClosed(m_nPortNo);
                LogEvenMsg("1803_Ev_BoxCarrierClosed");
            }
            else
            {
                SecsEquipHelper.Even.PosEvent(30075);
            }

            BoxCarrierClosed_B = true;
        }

        private bool BoxCarrierUnClamped_B;
        public void Ev_BoxCarrierUnClamped_R()
        {
            BoxCarrierUnClamped_B = false;
        }
        /// <summary>
        ///  Load Port解锁料盒
        /// </summary>
        /// <param name="portNO_KE"></param>
        /// <param name="portWaferStatus_KE"></param>
        /// <param name="portFoupID"></param>
        public void Ev_BoxCarrierUnClamped(PortFoupStatus_KE portFoupStatus_KE, string portFoupID)
        {
            if (BoxCarrierUnClamped_B) { return; }
            SetFoupValue((PortNO_KE)m_nPortNo, portFoupStatus_KE, portFoupID);
            if (SecsDataHelper.Function.Gem300Enable)
            {
                m_pSecs.CmsLoadPortUnclamped(m_nPortNo);
                LogEvenMsg("1801_Ev_BoxCarrierUnClamped");
            }
            else
            {
                SecsEquipHelper.Even.PosEvent(30076);
            }

            BoxCarrierUnClamped_B = true;
        }

        private bool LoadPortUnLoaded_B;
        public void Ev_LoadPortUnLoaded_R()
        {
            LoadPortUnLoaded_B = false;
        }
        /// <summary>
        /// 下料完成
        /// </summary>
        /// <param name="portNO_KE"></param>
        /// <param name="portWaferStatus_KE"></param>l
        /// <param name="portFoupID"></param>
        public void Ev_LoadPortUnLoaded(PortFoupStatus_KE portFoupStatus_KE, string portFoupID)
        {
            if (LoadPortUnLoaded_B) { return; }
            SetFoupValue((PortNO_KE)m_nPortNo, portFoupStatus_KE, portFoupID);
            if (m_nPortNo == 1)
            {
                SecsEquipHelper.Variables.SetValue(10190, ((int)PortStatus_KE.ReadyToLoad).ToString());
                SecsEquipHelper.Variables.SetValue(10354, "");
            }
            else if (m_nPortNo == 2)
            {
                SecsEquipHelper.Variables.SetValue(10191, ((int)PortStatus_KE.ReadyToLoad).ToString());
                SecsEquipHelper.Variables.SetValue(10355, "");
            }
            if (SecsDataHelper.Function.Gem300Enable)
            {
                var res = m_pSecs.CmsLoadPortUnloaded(m_nPortNo);
                LogEvenMsg("1836_Ev_LoadPortUnLoaded");
            }
            else
            {
                SecsEquipHelper.Even.PosEvent(30077);
            }

            LoadPortUnLoaded_B = true;
        }


        private bool LoadPortVerifyCarrier_2_B;
        public void Ev_VerifyCarrierMap_2_R()
        {
            LoadPortVerifyCarrier_2_B = false;
        }
        /// <summary>
        /// EAP下发S3F17 ProcessWithCarrier-2，设备校验结果
        /// </summary>
        /// <param name="carrierVerifyRes"></param>
        public void Ev_VerifyCarrierMapRes_2(string carrierVerifyRes, PortFoupStatus_KE portWaferStatus_KE, string portFoupID)
        {
            if (LoadPortVerifyCarrier_2_B) { return; }
            SecsEquipHelper.Variables.SetValue(10192, carrierVerifyRes);
            SecsEquipHelper.Variables.SetValue(10199, m_nPortNo.ToString());
            SecsEquipHelper.Variables.SetValue(10200, ((int)portWaferStatus_KE).ToString());
            SecsEquipHelper.Variables.SetValue(10201, portFoupID);
            SecsEquipHelper.Even.PosEvent(30023);
            LoadPortVerifyCarrier_2_B = true;
        }


        bool Ev_BoxCarrierProcessStart_B;
        public void Ev_BoxCarrierProcessStart_R()
        {
            Ev_BoxCarrierProcessStart_B = false;
        }
        /// <summary>
        /// ProcessStart
        /// </summary>
        /// <param name="portNO_KE"></param>
        /// <param name="portWaferStatus_KE"></param>
        /// <param name="portFoupID"></param>
        public void Ev_BoxCarrierProcessStart(PortFoupStatus_KE portWaferStatus_KE, string portFoupID)
        {
            if (Ev_BoxCarrierProcessStart_B) { return; }
            SecsEquipHelper.Variables.SetValue(10199, m_nPortNo.ToString());
            SecsEquipHelper.Variables.SetValue(10200, ((int)portWaferStatus_KE).ToString());
            SecsEquipHelper.Variables.SetValue(10201, portFoupID);
            SecsEquipHelper.Even.PosEvent(30011);
            Ev_BoxCarrierProcessStart_B = true;
        }

        bool Ev_BoxCarrierProcessEnd_B;
        public void Ev_BoxCarrierProcessEnd_R()
        {
            Ev_BoxCarrierProcessEnd_B = false;
        }
        /// <summary>
        /// ProcessEnd
        /// </summary>
        /// <param name="portNO_KE"></param>
        /// <param name="portWaferStatus_KE"></param>
        /// <param name="portFoupID"></param>
        public void Ev_BoxCarrierProcessEnd(PortFoupStatus_KE portWaferStatus_KE, string portFoupID)
        {
            if (Ev_BoxCarrierProcessEnd_B) { return; }
            SecsEquipHelper.Variables.SetValue(10199, m_nPortNo.ToString());
            SecsEquipHelper.Variables.SetValue(10200, ((int)portWaferStatus_KE).ToString());
            SecsEquipHelper.Variables.SetValue(10201, portFoupID);
            SecsEquipHelper.Even.PosEvent(30012);
            Ev_BoxCarrierProcessEnd_B = true;
        }

        private bool ReadCarrierSlotSucc_B;
        public void Ev_ReadCarrierSlotSucc_R()
        {
            ReadCarrierSlotSucc_B = false;
        }
        /// <summary>
        /// 卡匣Mapping结果
        /// </summary>
        /// <param name="portNO_KE"></param>
        /// <param name="portWaferStatus_KE"></param>
        /// <param name="portFoupID"></param>
        /// <param name="mapRet"></param>
        public void Ev_ReadCarrierSlotSucc(PortFoupStatus_KE portWaferStatus_KE, string portFoupID, List<ushort> MapDatas)
        {
            if (ReadCarrierSlotSucc_B) { return; }

            //for (int i = 0; i < 25; i++)
            //{
            //    if (MapDatas.Count < 25)
            //    {
            //        MapDatas.Add(0);
            //    }
            //}

            if (SecsDataHelper.Function.Gem300Enable)
            {
                if (portFoupID == null || portFoupID.Length == 0)
                {
                    LogEvenMsg($"Error: 卡匣I为空");
                }
                else
                {
                    List<SlotMap> vecMap = new List<SlotMap>();
                    for (int i = 0; i < MapDatas.Count; i++)
                    {
                        switch (MapDatas[i])
                        {
                            case 0:
                                {
                                    vecMap.Add(SlotMap.EMPTY);
                                }
                                break;
                            case 1:
                                {
                                    vecMap.Add(SlotMap.CORRECTLY_OCCUPIED);
                                }
                                break;
                            case 2:
                                {
                                    vecMap.Add(SlotMap.DOUBLESLOTTED);
                                }
                                break;
                            case 3:
                                {
                                    vecMap.Add(SlotMap.CROSS_SLOTTED);
                                }
                                break;
                            default:
                                {
                                    vecMap.Add(SlotMap.UNDEFINED);
                                }
                                break;
                        }
                    }
                    MapSlotResult = MapDatas;
                    RcResult rc = m_pSecs.CmsCarrierSlotReadSucc(m_nPortNo, portFoupID, vecMap);
                    LogEvenMsg("1830_Ev_ReadCarrierSlotSucc");
                    if (rc != 0)
                    {
                        LogEvenMsg($"Error: 1830_Ev_ReadCarrierSlot  {rc.rc.ToString()} : {rc.result.ToString()}");
                        MessageBox.Show($"Error: 1830_Ev_ReadCarrierSlot  {rc.rc.ToString()} : {rc.result.ToString()}");
                    }
                }
            }
            else
            {
                SecsEquipHelper.Variables.SetValue(10199, m_nPortNo.ToString());
                SecsEquipHelper.Variables.SetValue(10200, ((int)portWaferStatus_KE).ToString());
                SecsEquipHelper.Variables.SetValue(10201, portFoupID);

                List<int> nVIDList = SecsEquipHelper.Variables.GetVIDList(10202, 10226);
                for (int i = 0; i < 25; i++)
                {
                    if (i < MapDatas.Count)
                    {
                        SecsEquipHelper.Variables.SetValue(nVIDList[i], MapDatas[i].ToString());
                    }
                    else
                    {
                        SecsEquipHelper.Variables.SetValue(nVIDList[i], "0");
                    }
                }
                MapSlotResult = MapDatas;
                SecsEquipHelper.Even.PosEvent(30020);
            }

            #region 保存Port信息
            LoadPortBase.PortInfo[m_nPortNo].MapSlotResult = MapSlotResult;
            LoadPortBase.Save();
            #endregion

            ReadCarrierSlotSucc_B = true;
        }


        bool Ev_RecipeChangeSucc_B;
        public void Ev_RecipeChangeSucc_R()
        {
            Ev_RecipeChangeSucc_B = false;

        }
        /// <summary>
        /// Recipe切换成功
        /// </summary>
        /// <param name="portNO_KE"></param>
        /// <param name="portWaferStatus_KE"></param>
        /// <param name="portFoupID"></param>
        /// <param name="mapRet"></param>
        public void Ev_RecipeChangeSucc(string recipeName, string lotNO, List<int> sampleList)
        {
            if (Ev_RecipeChangeSucc_B) { return; }
            SecsEquipHelper.Variables.SetValue(10199, m_nPortNo.ToString());
            SecsEquipHelper.Variables.SetValue(10350, recipeName);
            SecsEquipHelper.Variables.SetValue(10351, lotNO);

            if (sampleList != null)
            {
                for (int i = 0; i < 13; i++)
                {
                    SecsEquipHelper.Variables.SetValue(10227 + i, sampleList[i].ToString());
                }
            }

            SecsEquipHelper.Even.PosEvent(30025);
            Ev_RecipeChangeSucc_B = true;
        }

        #endregion
        public void CheckStartScan(string pCJobID)
        {
            if (!SecsDataHelper.Function.Gem300Enable || SecsDataHelper.Function.IsStartByRCMD)
            {
                return;
            }
            if (!GlobalVar.StartScan[$"Port_{m_nPortNo}"])
            {
                if (SecsEquipHelper.CJob.IsCurCJobAuto(pCJobID))
                {
                    GlobalVar.StartScan[$"Port_{m_nPortNo}"] = true;
                    LogMsg($"Port_{m_nPortNo}当前选择的ID为：{SecsEquipHelper.CJob.CurSelCJob.pCJobID}的CJob, 控制状态为自动，开始作业。。。", MsgSource_KE.EQ);
                }
                else
                {
                    LogMsg($"Port_{m_nPortNo}当前选择的ID为：{SecsEquipHelper.CJob.CurSelCJob.pCJobID}的CJob, 控制状态未自动，请确认EAP下发的CJob信息!", MsgSource_KE.EQ);
                }
            }
        }

        public string CarrierStatus = "";
        public REMOTE_REPLY_KE ONAccepCarrierIDStatus(string str)
        {
            CarrierStatus = "Port" + str + "AcceptCarrier";
            return REMOTE_REPLY_KE.eCOMMAND_PERFORMED;
        }
        public REMOTE_REPLY_KE ONRejectCarrierIDStatus(string str)
        {
            CarrierStatus = "Port" + str + "RejectCarrier";
            return REMOTE_REPLY_KE.eCOMMAND_PERFORMED;
        }
        public string RecipeID_SJ;
        public string LotID_SJ;
        public REMOTE_REPLY_KE ONGetPPID(string pRecipeName, string pLotName)
        {
            RecipeID_SJ = pRecipeName;
            LotID_SJ = pLotName;
            return REMOTE_REPLY_KE.eCOMMAND_PERFORMED;
        }
        public string StrStart = "";
        public REMOTE_REPLY_KE ONStartForEap(string str)
        {
            StrStart = "Port" + str + "Start";
            return REMOTE_REPLY_KE.eCOMMAND_PERFORMED;
        }

        private void LogEvenMsg(string Log)
        {
            string portNO = m_nPortNo == 1 ? "PA" : "PB";
            string strLog = string.Format($"Port事件: {portNO} -> {Log}");
            LogMsg(strLog, MsgSource_KE.EQ);
        }
    }
}
